/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package frontend;

import dao.impl.UserDAOImpl;
import entities.UserVO;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import validator.FormValidator;
import validator.ValidatorException;

/**
 *
 * @author Valentina
 */
public class UserAddServlet extends HttpServlet {

    protected List<ValidatorException> myErrors = new ArrayList<ValidatorException>();
    protected List mySuccesMsg = new ArrayList();
    protected Boolean isValidated = true;
    
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String userName = request.getParameter("name");
        String userLogin = request.getParameter("login");
        String userPassword = request.getParameter("password");
        String idGroup = request.getParameter("idGroup");
        
        Long maxId;
        
        myErrors.removeAll(myErrors);
        RequestDispatcher rd = null;
        

        UserDAOImpl userDAOImpl = new UserDAOImpl();
        UserVO user = new UserVO();

        isRequired(request, userName, "Name");
        isRequired(request, userLogin, "Login");
        isRequired(request, userPassword, "Password");
        isValidPassword(request, userPassword);
        ifLoginExist(request, userLogin);

        if (myErrors.isEmpty()) {
            user.setName(userName);
            user.setLogin(userLogin);
            user.setPassword(userPassword);
            user.setIdGroup(Long.parseLong(idGroup));
            maxId = userDAOImpl.create(user);
            
            if(maxId>0){
                mySuccesMsg.add("Record Added Successfully");
                request.setAttribute("mySuccesMsg", mySuccesMsg);
            }else{
                myErrors.add(new ValidatorException("Error:: Record was not added"));
            }
        }

        rd = request.getRequestDispatcher("UserServlet");
        rd.forward(request, response);
    }

    private boolean ifLoginExist(HttpServletRequest request, String name){
        
        UserDAOImpl userDAOImpl = new UserDAOImpl();
        
        if (!userDAOImpl.isUnic(name)) {
            myErrors.add(new ValidatorException("Login exist"));
        }

        return returnErrorSize(request, myErrors);
    }
    
    private boolean isValidPassword(HttpServletRequest request, String password){
        
        if (!FormValidator.validatePasswordField(password)) {
            myErrors.add(new ValidatorException("Password is to small"));
        }

        return returnErrorSize(request, myErrors);
    }
    
    
    private boolean isRequired(HttpServletRequest request, String name, String field) {

        if (!FormValidator.validateRequiredField(name)) {
            myErrors.add(new ValidatorException("Field "+field+" is required!"));
        }

        return returnErrorSize(request, myErrors);
    }
    
    private boolean returnErrorSize(HttpServletRequest request, List<ValidatorException> myErrors){
        
         if (myErrors.size() > 0) {
            request.setAttribute("myErrors", myErrors);
            isValidated = false;
        }
         
        return isValidated;
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
